﻿using YiShop.System.Services;

namespace YiShop.System.Controllers
{
    [Route("system/menu")]
    public class SysMenuController : ControllerBase
    {
        private readonly SysMenuService _sysMenuService;
        public SysMenuController(SysMenuService menuService)
        {
            _sysMenuService = menuService;
        }

        /// <summary>
        /// 查询菜单权限表列表
        /// </summary>
        [HttpGet("list")]
        public async Task<AjaxResult> SysMenuListAsync([FromQuery] SysMenuDto dto)
        {
            var data = await _sysMenuService.GetListAsync(dto);
            return AjaxResult.Success(data);
        }

        /// <summary>
        /// 获取菜单下拉树列表
        /// </summary>
        [HttpGet("treeselect")]
        public async Task<AjaxResult> Treeselect([FromQuery] SysMenuDto dto)
        {
            var menus = await _sysMenuService.GetListAsync(dto);
            var data = _sysMenuService.BuildMenuTree(menus);
            return AjaxResult.Success(data);
        }

        /// <summary>
        /// 加载对应角色菜单列表树
        /// </summary>
        [HttpGet("roleMenuTreeselect/{roleId}")]
        public async Task<AjaxResult> RoleMenuTreeselectAsync(long roleId)
        {
            List<SysMenu> menus = await _sysMenuService.GetListAsync(new SysMenuDto());
            var ajax = AjaxResult.Success();
            ajax.Add("checkedKeys", await _sysMenuService.SelectMenuListByRoleId(roleId));
            ajax.Add("menus", _sysMenuService.BuildMenuTree(menus));
            return ajax;
        }
    }
}